<template>
  <div v-theme:column="'narrow'" id="show-blogs">
    <h1>博客总览</h1>
    <input type="text" v-model="search" placeholder="搜索" />
    <div v-for="blog in filteredBlogs" class="single-blog" :key="blog.id">
      <router-link v-bind:to="'/blog/'+blog.id">
        <h2 v-rainbow>{{blog.title|to-uppercase}}</h2>
      </router-link>
      <article>{{blog.content|sinppet}}</article>
    </div>
  </div>
</template>

<script>
import axios from "../axios-auth";
export default {
  name: "show-blogs",
  data() {
    return {
      blogs: [],
      search: ""
    };
  },
  created() {

      /* .get("https://jsonplaceholder.typicode.com/posts") */
      /* this.$http.get("https://vuedemo-8dc9d.firebaseio.com/posts.json") */
      axios.get("/posts.json")
      .then(function(data) {
        /* return data.json(); */
        /*   this.blogs = data.body.slice(0, 10); */
        return data.data;
      })
      .then((data)=> {
        var blogsArray = [];
        for (let key in data) {
          console.log(key);
           console.log(data[key]);
           data[key].id=key;
           blogsArray.push(data[key]);
        }
        console.log(blogsArray);
        this.blogs=blogsArray;
      });
  },
  computed: {
    filteredBlogs: function() {
      var obj = this;
      return this.blogs.filter(function(blog) {
        return blog.title.match(obj.search);
      });
    }
  },
  filters: {
    /* "to-uppercase": function(value) {
      return value.toUpperCase();
    } */
    toUppercase(value) {
      return value.toUpperCase();
    }
  },
  directives: {
    /* 自定义指令 */
    rainbow: {
      bind(el, binding, vnode) {
        el.style.color =
          "#" +
          Math.random()
            .toString(16)
            .slice(2, 8);
      }
    }
  }
};
</script>

<style scoped>
#show-blogs {
  max-width: 800px;
  margin: 0 auto;
}
.single-blog {
  padding: 20px;
  margin: 20px 0;
  box-sizing: border-box;
  background: #cccccc;
  border: 1px dotted #aaaaaa;
}
#show-blogs a {
  text-decoration: none;
}
input[type="text"] {
  padding: 8px;
  width: 100%;
  box-sizing: border-box;
}
</style>
